﻿using System;
using System.Collections.Generic;
using ApplicationLibrary;
using ApplicationLibrary.CounterManager;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;
using Plumber.Sales.Reports.DataSet;
using Plumber.Accounting.ModuleData;

namespace Plumber.Sales.Reports
{
    public partial class SalesSummary : ApplicationLibrary.DBManager.ReportForm
    {
        private DRMasters drMasters = null;
        private DRTitle drTitle = null;
        private DRMasterType DRMasterType;
        private DRCreditNoteByInvoices drCreditNote;
        private DRJournalEntryForPaymentSales drSalesPayed;
        private ComboBoxManager cbbManager;
        private string condomType, adminType;
        private MasterTypes masterType;
        private CounterFormatter formatter = new CounterFormatter();
        private string Title = string.Empty;
        private List<string> codes = new List<string>();

        public SalesSummary()
            : base(Properties.Resources.FN_SalesSummary)
        {
            InitializeComponent();
        }

        protected override void OnBatchExecute(string reportName)
        {
            QueryBuilder qb = new QueryBuilder();
            SqlABCommand scc = new SqlABCommand(base.providerType);
            scc.Connection = GlobalInfo.SqlConnection;

            formatter.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_SaleInvoices), GlobalInfo.CurrentDate);

            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);

            //if (!ckbAllInvoice.Checked && !rdbSelectDate.Checked)
            //    qb.AddCompare<bool>(PL_SalesInvoices.SalePayed, ckbPayed.Checked);

            if (rdbSumOnly.Checked)
            {
                SqlABParameter sType = new SqlABParameter("@p8", PL_SalesInvoices.Type);
                scc.Parameters.Add(sType);
                qb.AddCompare(PL_SalesInvoices.Type, sType);
                sType.Value = cbbManager.GetValue<string>();
            }

            if (rdbSelectDate.Checked)
            {
                SqlABParameter sFrom = new SqlABParameter("@p1", PL_SalesInvoices.ReleaseDate);
                sFrom.Value = ctbFromDate.Today;
                scc.Parameters.Add(sFrom);
                SqlABParameter sTo = new SqlABParameter("@p2", PL_SalesInvoices.ReleaseDate);
                sTo.Value = ctbToDate.Today;
                scc.Parameters.Add(sTo);
                qb.AddBetween(PL_SalesInvoices.ReleaseDate, sFrom, sTo);
            }

            if (ckbPayed.Checked || ckbToPayed.Checked || ckbPartialPayed.Checked)
            {
                string pQuery = "( ";
                
                QueryBuilder qb2 = new QueryBuilder();

                if (ckbPayed.Checked)
                {
                    SqlABParameter sPayed = new SqlABParameter("@p9", PL_SalesInvoices.PaymentDate);
                    qb2.AddCompare(PL_SalesInvoices.PaymentDate, sPayed, "<=");
                    sPayed.Value = ctbPaymentDate.Today;
                    scc.Parameters.Add(sPayed);
                    pQuery += qb2.Where;
                }

                if (ckbToPayed.Checked)
                {
                    qb2.Clear();

                    SqlABParameter sToPayed = new SqlABParameter("@p10", PL_SalesInvoices.PaymentDate);
                    qb2.AddManualFilter("({0} = {1} OR {2} > {3})",
                        PL_SalesInvoices.PaymentDate.Name, "'17991231'",
                        PL_SalesInvoices.PaymentDate.Name, sToPayed);
                    sToPayed.Value = ctbPaymentDate.Today;
                    scc.Parameters.Add(sToPayed);
                    if (pQuery.Length == 2)
                        pQuery += qb2.Where;
                    else
                        pQuery += string.Format(" OR ( {0} )", qb2.Where);
                }

                if (ckbPartialPayed.Checked)
                {
                    qb2.Clear();

                    //SqlABParameter sPartPayed = new SqlABParameter("@p11", PL_SalesInvoices.PaymentDate);
                    //qb2.AddCompare(PL_SalesInvoices.PaymentDate, sPartPayed, "<=");
                    //sPartPayed.Value = ctbPaymentDate.Today;
                    //scc.Parameters.Add(sPartPayed);

                    SqlABParameter sAmountPayed = new SqlABParameter("@p12", PL_SalesInvoices.AmountPayed);
                    qb2.AddCompare(PL_SalesInvoices.AmountPayed, sAmountPayed, "!=");
                    sAmountPayed.Value = 0;
                    scc.Parameters.Add(sAmountPayed);

                    qb2.AddManualFilter("{0} != {1}",
                        PL_SalesInvoices.Total.Name, PL_SalesInvoices.AmountPayed.Name);

                    if (pQuery.Length == 2)
                        pQuery += qb2.Where;
                    else
                        pQuery += string.Format(" OR ( {0} )", qb2.Where);
                }

                pQuery += " )";
                qb.AddFilter(pQuery);
            }

            if (rdbSelectAdmin.Checked)
            {
                SqlABParameter sFrom = new SqlABParameter("@p3", PL_SalesInvoices.Administrator);
                sFrom.Value = rdrAdminFrom.Text;
                scc.Parameters.Add(sFrom);
                SqlABParameter sTo = new SqlABParameter("@p4", PL_SalesInvoices.Administrator);
                sTo.Value = rdrAdminTo.Text;
                scc.Parameters.Add(sTo);
                qb.AddBetween(PL_SalesInvoices.Administrator, sFrom, sTo);
                SqlABParameter sNo = new SqlABParameter("@p5", PL_SalesInvoices.Administrator);
                sNo.Value = "";
                scc.Parameters.Add(sNo);
                qb.AddCompare(PL_SalesInvoices.Administrator, sNo, "<>");
            }

            if (rdbSelectMaster.Checked && rdbSumOnly.Checked)
            {
                SqlABParameter sFrom = new SqlABParameter("@p6", PL_SalesInvoices.Master);
                sFrom.Value = rdrMasterFrom.Text;
                scc.Parameters.Add(sFrom);
                SqlABParameter sTo = new SqlABParameter("@p7", PL_SalesInvoices.Master);
                sTo.Value = rdrMasterTo.Text;
                scc.Parameters.Add(sTo);
                qb.AddBetween(PL_SalesInvoices.Master, sFrom, sTo);
            }

            if (rdbSumOnly.Checked)
                qb.AddOrder(PL_SalesInvoices.Administrator);
            qb.AddOrder(string.Format("YEAR({0})", PL_SalesInvoices.ReleaseDate.Name));
            qb.AddOrder(PL_SalesInvoices.InvoiceNo);

            scc.CommandText = qb.Query;

            SqlABDataReader dr = scc.ExecuteReader();

            DSInvoiceSummary DSInvSum = new DSInvoiceSummary();
            codes.Clear();
            int cnt = 0;
            while (dr.Read())
            {
                cnt++;
                string code = "";
                string typeAnalized;

                if (dr.GetValue<string>(PL_SalesInvoices.Type) == condomType)
                {
                    typeAnalized = adminType;
                    code = dr.GetValue<string>(PL_SalesInvoices.Administrator);
                }
                else
                {
                    typeAnalized = dr.GetValue<string>(PL_SalesInvoices.Type);
                    code = dr.GetValue<string>(PL_SalesInvoices.Master);
                }
                string desc = "";
                if (code == "")
                    code = dr.GetValue<string>(PL_SalesInvoices.Description).Replace(" ", "").Replace("\t", "").Replace("\r\n", "");

                if (code == "")
                    code = " ";
                if (code != "" && !codes.Contains(code) /*&& (rdbSumOnly.Checked || rdbLetterHead.Checked)*/)
                {
                    codes.Add(code);

                    if (drMasters.Find(typeAnalized, code))
                    {
                        desc = drMasters.GetValue<string>(PL_Masters.Description);
                        string address = drMasters.GetValue<string>(PL_Masters.Address);
                        string zip = drMasters.GetValue<string>(PL_Masters.ZipCode);
                        string city = drMasters.GetValue<string>(PL_Masters.City);
                        string county = drMasters.GetValue<string>(PL_Masters.County);
                        if (drTitle.Find(drMasters.GetValue<string>(PL_Masters.Title)))
                            desc = drTitle.GetValue<string>(PL_Titles.Description) + "\n" + desc;

                        desc = string.Concat(desc, "\n", address, "\n", zip, " ", city, " ", county);
                    }
                    else
                    {
                        desc = dr.GetValue<string>(PL_SalesInvoices.Description);
                        string address = dr.GetValue<string>(PL_SalesInvoices.Address);
                        string zip = dr.GetValue<string>(PL_SalesInvoices.ZipCode);
                        string city = dr.GetValue<string>(PL_SalesInvoices.City);
                        string county = dr.GetValue<string>(PL_SalesInvoices.County);
                        if (drTitle.Find(dr.GetValue<string>(PL_SalesInvoices.Title)))
                            desc = drTitle.GetValue<string>(PL_Titles.Description) + "\n" + desc;

                        desc = string.Concat(desc, "\n", address, "\n", zip, " ", city, " ", county);
                    }

                    DSInvoiceSummary.MasterRow dHeader = DSInvSum.Master.NewMasterRow();
                    dHeader["Code"] = code;
                    dHeader["Description"] = desc;
                    DSInvSum.Master.AddMasterRow(dHeader);
                }

                // Add Detail
                DSInvoiceSummary.SalesInvoiceRow dRow = DSInvSum.SalesInvoice.NewSalesInvoiceRow();
                if (rdbSumOnly.Checked || rdbLetterHead.Checked)
                    dRow["Code"] = code;
                else
                    dRow["Code"] = " ";

                if (dr.GetValue<InvoiceType>(PL_SalesInvoices.InvoiceType) == InvoiceType.E_CreditNoteEm)
                    continue;

                switch (dr.GetValue<InvoiceType>(PL_SalesInvoices.InvoiceType))
                {
                    case InvoiceType.E_CreditNoteEm:
                        dRow["InvoiceType"] = "NC";
                        break;
                    case InvoiceType.E_InvoiceEm:
                        dRow["InvoiceType"] = "FT";
                        break;
                    case InvoiceType.E_DebitNoteEm:
                        dRow["InvoiceType"] = "ND";
                        break;

                }
                dRow["ReleaseDate"] = dr.GetValue<DateTime>(PL_SalesInvoices.ReleaseDate);
                dRow["Description"] = dr.GetValue<string>(PL_SalesInvoices.Type) == condomType
                                ? dr.GetValue<string>(PL_SalesInvoices.Address)
                                : dr.GetValue<string>(PL_SalesInvoices.Description);
                formatter.Text = dr.GetValue<string>(PL_SalesInvoices.InvoiceNo);
                dRow["SaleNo"] = formatter.ToString(false);
                dRow["RestraintAmount"] = dr.GetValue<double>(PL_SalesInvoices.RestraintAmount);
                if (ckbToPayed.Checked)
                    dRow["Total"] = dr.GetValue<double>(PL_SalesInvoices.Total) -
                                    dr.GetValue<double>(PL_SalesInvoices.AmountPayed);
                else
                    dRow["Total"] = dr.GetValue<double>(PL_SalesInvoices.Total);
                dRow["Amount"] = dr.GetValue<double>(PL_SalesInvoices.Amount);
                dRow["Tax"] = dr.GetValue<double>(PL_SalesInvoices.Tax);
                dRow["TaxCode"] = dr.GetValue<string>(PL_SalesInvoices.TaxCode) == ""
                                ? ""
                                : dr.GetValue<string>(PL_SalesInvoices.TaxCode) + " %";

                if (drCreditNote.Find(formatter.Text))
                {
                   // if (drCreditNote.GetValue<double>(PL_SalesInvoices.Total) == dr.GetValue<double>(PL_SalesInvoices.Total))
                        continue;
                }

                DSInvSum.SalesInvoice.AddSalesInvoiceRow(dRow);

                if (ckbAlsoPayment.Checked)
                    PrintPayment(DSInvSum,
                        code,
                        dr.GetValue<string>(PL_SalesInvoices.Type),
                        dr.GetValue<string>(PL_SalesInvoices.Master),
                        dr.GetValue<string>(PL_SalesInvoices.InvoiceNo),
                        dRow.SaleNo);
            }
            dr.Close();

            string invoiceStatus = "";

            if (ckbPayed.Checked)
                invoiceStatus = invoiceStatus.SeparConcat(Properties.Resources.RPT_Payed, " ");
            if (ckbPartialPayed.Checked)
                invoiceStatus = invoiceStatus.SeparConcat(Properties.Resources.RPT_PartialPayed, ", ");
            if (ckbToPayed.Checked)
                invoiceStatus = invoiceStatus.SeparConcat(Properties.Resources.RPT_ToPayed, ", ");

            if (!invoiceStatus.IsEmpty())
                invoiceStatus = string.Format("{0} {1} {2}", invoiceStatus, Properties.Resources.RPT_At, ctbPaymentDate.Text);

            Title = string.Format(Properties.Resources.RPT_Summarized, invoiceStatus);

            if (rdbSelectDate.Checked)
                Title += string.Format(Properties.Resources.RPT_FromDate, ctbFromDate.Text, ctbToDate.Text);

            SetParameterValue("TitleReport", Title);
            if (rdbSumAll.Checked && !rdbLetterHead.Checked)
                ShowReport(new InvoiceCommSumm(), DSInvSum);
            else
            {
                SetParameterValue("HideLogo", rdbLetterHead.Checked);
                if (masterType == MasterTypes.E_Condominium)
                {
                    ShowReport(new InvoiceSummaryAdmin(), DSInvSum);
                }
                else
                    ShowReport(new InvoiceSummaryAll(), DSInvSum);
            }
        }

        private void PrintPayment(DSInvoiceSummary DSInvSum, string code, string type, string master, string docNo, string salesNo)
        {
            if (drSalesPayed.Find(type, master, docNo))
            {
                for (int t = 0; t < drSalesPayed.Count; t++)
                {
                    DSInvoiceSummary.SalesInvoiceRow dRow = DSInvSum.SalesInvoice.NewSalesInvoiceRow();
                    dRow["ReleaseDate"] = drSalesPayed.GetValue<DateTime>(PL_JournalEntry.PostingDate);
                    dRow["Description"] = "Incassata fattura N°" + docNo;
                    dRow["Total"] = drSalesPayed.GetValue<double>(PL_JournalEntryDetail.From);
                    dRow["SaleNo"] = "";
                    dRow["InvoiceType"] = "";
                    dRow["Code"] = code != null ? code : "";
                    dRow["Amount"] = 0;
                    dRow["Tax"] = 0;
                    dRow["TaxCode"] = "";
                    dRow["RestraintAmount"] = 0;
                    DSInvSum.SalesInvoice.AddSalesInvoiceRow(dRow);
                }
            }
        }

        protected override void OnDisableControlsForRun()
        {
            ctbFromDate.Enabled = rdbSelectDate.Checked;
            ctbToDate.Enabled = rdbSelectDate.Checked;

            rdbAllMaster.Enabled = rdbSumOnly.Checked;
            rdbSelectMaster.Enabled = rdbSumOnly.Checked;
            rdrMasterFrom.Enabled = rdbSumOnly.Checked && rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbSumOnly.Checked && rdbSelectMaster.Checked;

            rdbAllAdmin.Enabled = rdbSelectAdmin.Enabled = rdbSumOnly.Checked && masterType == MasterTypes.E_Condominium;
            rdrAdminFrom.Enabled = rdrAdminTo.Enabled = rdbSumOnly.Checked && rdbSelectAdmin.Checked && masterType == MasterTypes.E_Condominium;

            cbbType.Enabled = rdbSumOnly.Checked;
        }

        protected override void OnAttachData()
        {
            RRMasterTypeForInvoice rrMasterType = new RRMasterTypeForInvoice();
            rrMasterType.Find();
            cbbManager = new ComboBoxManager();
            for (int t = 0; t < rrMasterType.Count; t++)
                if (rrMasterType.GetValue<MasterTypes>(PL_MasterType.MasterType, t) != MasterTypes.E_Administrator)
                    cbbManager.AddValue(rrMasterType.GetValue<string>(PL_MasterType.Code, t), rrMasterType.GetValue<string>(PL_MasterType.Description, t));
            cbbManager.AttachTo(cbbType);

            DRMasterTypeByType dRMasterTypeByType = new DRMasterTypeByType();

            if (dRMasterTypeByType.Find(MasterTypes.E_Condominium))
            {
                condomType = dRMasterTypeByType.GetValue<string>(PL_MasterType.Code, 0);

                rdrMasterFrom.AttachCodeType(condomType);
                rdrMasterFrom.RadarForm = new RadarMasterForm(condomType, RadarMasterForm.Module.Master);
                rdrMasterFrom.EnableAddOnFly = false;
                rdrMasterFrom.MustExistData = false;

                rdrMasterTo.AttachCodeType(condomType);
                rdrMasterTo.RadarForm = new RadarMasterForm(condomType, RadarMasterForm.Module.Master);
                rdrMasterTo.EnableAddOnFly = false;
                rdrMasterTo.MustExistData = false;

                cbbType.SelectedValue = condomType;
            }

            if (dRMasterTypeByType.Find(MasterTypes.E_Administrator))
            {
                adminType = dRMasterTypeByType.GetValue<string>(PL_MasterType.Code);

                rdrAdminFrom.AttachCodeType(adminType);
                rdrAdminFrom.RadarForm = new RadarMasterForm(adminType, RadarMasterForm.Module.Master);
                rdrAdminFrom.EnableAddOnFly = false;
                rdrAdminFrom.MustExistData = false;

                rdrAdminTo.AttachCodeType(adminType);
                rdrAdminTo.RadarForm = new RadarMasterForm(adminType, RadarMasterForm.Module.Master);
                rdrAdminTo.EnableAddOnFly = false;
                rdrAdminTo.MustExistData = false;
            }

            drMasters = new DRMasters(false);
            drTitle = new DRTitle(false);
            DRMasterType = new DRMasterType(DRMasterType.Module.All);
            drCreditNote = new DRCreditNoteByInvoices(false);

            drSalesPayed = new DRJournalEntryForPaymentSales();

            AddClickableColumn(PL_SalesInvoices.InvoiceNo);

            base.OnAttachData();
        }

        protected override void OnBindData()
        {
            BindControl(rdrAdminFrom);
            BindControl(rdrAdminTo);
        }

        protected override void OnDoubleClickColumn(string column, string value, CrystalDecisions.Windows.Forms.ObjectInfo[] objectInfo)
        {
            //formatter.InvertedSuffixPrefix = true;
            formatter.Text = value;
            //formatter.InvertedSuffixPrefix = false;
            var tipoDoc = objectInfo[8].Text;

            if (tipoDoc == "FT")
            {
                Sales.Forms.SalesInvoice frm = (Sales.Forms.SalesInvoice)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.SalesInvoice", false);
                frm.FindRecord(new RadarSaleInvoicesParam(formatter.ToString()));
            }
            else if (tipoDoc == "NC")
            {
                Sales.Forms.CreditNote frm = (Sales.Forms.CreditNote)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.CreditNote", false);
                frm.FindRecord(new RadarSaleInvoicesParam(formatter.ToString()));
            }
            else if (tipoDoc == "ND")
            {
                Sales.Forms.CreditNote frm = (Sales.Forms.CreditNote)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.DebitNote", false);
                frm.FindRecord(new RadarSaleInvoicesParam(formatter.ToString()));
            }
        }

        private void rdbRangeDate_CheckedChanged(object sender, EventArgs e)
        {
            ctbFromDate.Enabled = rdbSelectDate.Checked;
            ctbToDate.Enabled = rdbSelectDate.Checked;
        }

        private void rdbRangeMaster_CheckedChanged(object sender, EventArgs e)
        {
            rdrMasterFrom.Enabled = rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbSelectMaster.Checked;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            rdrAdminFrom.Enabled = rdbSelectAdmin.Checked;
            rdrAdminTo.Enabled = rdbSelectAdmin.Checked;
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DRMasterType == null || cbbManager.GetValue<string>() == default(string))
                return;

            CodeMode codeMode = CodeMode.E_MustExists;

            if (DRMasterType.Find(cbbManager.GetValue<string>()))
            {
                codeMode = DRMasterType.GetValue<CodeMode>(PL_MasterType.CodeMode);
                masterType = DRMasterType.GetValue<MasterTypes>(PL_MasterType.MasterType);
            }

            rdbAllAdmin.Enabled = rdbSelectAdmin.Enabled = rdbSumOnly.Checked && masterType == MasterTypes.E_Condominium;
            rdrAdminFrom.Enabled = rdrAdminTo.Enabled = rdbSumOnly.Checked && rdbSelectAdmin.Checked && masterType == MasterTypes.E_Condominium;

            rdrMasterFrom.ReattachCodeType(cbbManager.GetValue<string>());
            rdrMasterTo.ReattachCodeType(cbbManager.GetValue<string>());

            rdrMasterFrom.RadarForm.FilterType = cbbManager.GetValue<string>();
            rdrMasterTo.RadarForm.FilterType = cbbManager.GetValue<string>();
        }

        private void rdrFrom_Validated(object sender, EventArgs e)
        {
            if (rdrMasterTo.Text.CompareTo(rdrMasterFrom.Text) < 0)
                rdrMasterTo.Text = rdrMasterFrom.Text;
        }

        private void rdrTo_Validated(object sender, EventArgs e)
        {
            if (rdrMasterTo.Text.CompareTo(rdrMasterFrom.Text) < 0)
                rdrMasterFrom.Text = rdrMasterTo.Text;
        }

        private void rdrAdminFrom_Validated(object sender, EventArgs e)
        {
            if (rdrAdminTo.Text.CompareTo(rdrAdminFrom.Text) < 0)
                rdrAdminTo.Text = rdrAdminFrom.Text;
        }

        private void rdrAdminTo_Validated(object sender, EventArgs e)
        {
            if (rdrAdminTo.Text.CompareTo(rdrAdminFrom.Text) < 0)
                rdrAdminFrom.Text = rdrAdminTo.Text;
        }

        private void ctbFromDate_Validated(object sender, EventArgs e)
        {
            if (ctbToDate.Today.IsEmpty() || ctbToDate.Today < ctbFromDate.Today)
            {
                int year = ctbFromDate.Today.Year;
                int month = ctbFromDate.Today.Month;
                int mday = DateTime.DaysInMonth(year, month);
                ctbToDate.Today = new DateTime(year, month, mday);
            }
        }

        private void ctbToDate_Validated(object sender, EventArgs e)
        {
            if (ctbToDate.Today < ctbFromDate.Today)
            {
                int year = ctbToDate.Today.Year;
                int month = ctbToDate.Today.Month;
                ctbFromDate.Today = new DateTime(year, month, 1);
            }
        }

        private void rdbSumOnly_CheckedChanged(object sender, EventArgs e)
        {
            if (DRMasterType.Find(cbbManager.GetValue<string>()))
                masterType = DRMasterType.GetValue<MasterTypes>(PL_MasterType.MasterType);

            rdbAllMaster.Enabled = rdbSumOnly.Checked;
            rdbSelectMaster.Enabled = rdbSumOnly.Checked;
            rdrMasterFrom.Enabled = rdbSumOnly.Checked && rdbSelectMaster.Checked;
            rdrMasterTo.Enabled = rdbSumOnly.Checked && rdbSelectMaster.Checked;
            rdbAllAdmin.Enabled = rdbSelectAdmin.Enabled = rdbSumOnly.Checked && masterType == MasterTypes.E_Condominium;

            rdrAdminFrom.Enabled = rdrAdminTo.Enabled = rdbSumOnly.Checked && rdbSelectAdmin.Checked && masterType == MasterTypes.E_Condominium;

            cbbType.Enabled = rdbSumOnly.Checked;
            if (rdbSumAll.Checked)
                masterType = MasterTypes.E_Other;
        }

        private void rdbPayed_CheckedChanged(object sender, EventArgs e)
        {
            //gpbDate.Text = ckbPayed.Checked
            //    ? Properties.Resources.T_PaymentDate
            //    : Properties.Resources.T_ReleaseDate;
        }

        protected override EmailArgs SendEmail()
        {
            EmailArgs ea = new EmailArgs();
            ea.Subject = Title;
            ea.Attachment = ExportToPdf(Title);
            return ea;
        }

        private void ckbToPayed_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbToPayed.Checked && ckbPartialPayed.Checked && ckbPayed.Checked)
            {
                ckbToPayed.Checked = ckbPartialPayed.Checked = ckbPayed.Checked = false;
                ckbAllInvoice.Checked = true;
                return;
            }

           ckbAllInvoice.Checked =  !(ckbToPayed.Checked | ckbPartialPayed.Checked | ckbPayed.Checked);
           ctbPaymentDate.Enabled = ckbToPayed.Checked | ckbPayed.Checked;
        }

        private void ckbAllInvoice_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbAllInvoice.Checked)
                ckbToPayed.Checked = ckbPartialPayed.Checked = ckbPayed.Checked = false;

            ctbPaymentDate.Enabled = ckbToPayed.Checked | ckbPayed.Checked;
        }
    }
}
